package com.example.ymm.modules1.service.impl.hl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.dkn.DknProductMapping;
import com.example.ymm.modules1.mapper.hl.DknProductMappingMapper;
import com.example.ymm.modules1.query.hl.DknProductMappingQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.DknProductMappingDto;
import com.example.ymm.modules1.service.dto.heng.ImportDknProductDto;
import com.example.ymm.modules1.service.dto.ms.MsOrderListQueryCriteria;
import com.example.ymm.modules1.service.hl.DknProductMappingService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class DknProductMappingServiceImpl extends BaseServiceImpl<DknProductMappingMapper, DknProductMapping> implements DknProductMappingService {

    @Override
    public TableList<DknProductMapping> list(DknProductMappingQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DknProductMapping> page = new Page<DknProductMapping>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknProductMapping.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DknProductMapping> tableList=new TableList(page);
        return tableList;
    }

    public List<DknProductMapping> queryAll(DknProductMappingQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknProductMapping.class, criteria);
        predicate.orderByDesc("create_time");
        List<DknProductMapping> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public DknProductMapping findByItemCode(String itemCode) {
        DknProductMappingQueryCriteria criteria=new DknProductMappingQueryCriteria();
        criteria.setItemCode(itemCode);
        List<DknProductMapping> list = queryAll(criteria);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<String> importDknProduct(ImportDknProductDto dto) {
        List<DknProductMapping> list = dto.getList();
        if(UtilValidate.isEmpty(list)){
            throw new YmException("参数异常!");
        }
        List<String> msg = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            DknProductMapping mapping=list.get(i);
            if(UtilValidate.isEmpty(mapping.getMtid())){
                msg.add("第"+(i+1)+"行 ms物料 编码不能为空！");
            }
            if(UtilValidate.isEmpty(mapping.getItemCode())){
                msg.add("第"+(i+1)+"行 迪卡侬 物料编码不能为空！");
            }
            String type="";
            if(UtilValidate.isNotEmpty(mapping.getType())){
                switch (mapping.getType()){
                    case "配件":
                        type="component";
                        break;
                    case "防盗贴纸":
                        type="carelabel";
                        break;
                }
            }
            mapping.setType(type);
        }

        if(UtilValidate.isEmpty(msg)){
            saveOrUpdateBatch(list);
        }
        return msg;
    }


    @Override
    public List<DknProductMappingDto> purchaseOrderList(MsOrderListQueryCriteria criteria) {
        return baseMapper.purchaseOrderList(criteria);
    }


    @Override
    public String purchaseOrderListMaxTime(MsOrderListQueryCriteria criteria) {
        return baseMapper.purchaseOrderListMaxTime(criteria);
    }

    @Override
    public List<DknProductMappingDto> purchaseOrderNoMappingList(MsOrderListQueryCriteria criteria) {
        return baseMapper.purchaseOrderNoMappingList(criteria);
    }


}
